package com.bjqh.edu.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Paint;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bjqh.edu.Base.AppConstants;
import com.bjqh.edu.Base.OrderPopupWindow;
import com.bjqh.edu.Bean.Course;
import com.bjqh.edu.Bean.HistoricalGrade;
import com.bjqh.edu.R;
import com.bjqh.edu.UI.Activity.StudentPersonal.HistoricalGradeActivity;
import com.bjqh.edu.UI.Activity.StudentPersonal.PaymentActivity;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/9/15.
 */

public class CoursePurchaseAdapter extends BaseAdapter {

    public void setList(List<Course.RowsBean> list) {
        this.list = list;
    }

    private List<Course.RowsBean> list;
    private Activity mActivity;
    private ViewHolder holder;
    private OrderPopupWindow orderPopupWindow;
    Course.RowsBean rowsBean;
    String price;
    String coursename;
    String coursedes;
    String cid;
    Double op;
    Double pp;

    public CoursePurchaseAdapter(Activity mActivity, List<Course.RowsBean> list) {
        this.mActivity = mActivity;
        this.list = list;

    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override

    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, final ViewGroup parent) {

        if (view == null) {
            view = LayoutInflater.from(mActivity).inflate(R.layout.item_course_purchase, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.tvOp.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        op=list.get(position).getPrice();
        try {
            Float count = new BigDecimal(list.get(position).getDiscount()).floatValue();
            pp =count/10*op;
            DecimalFormat df   = new DecimalFormat("#0.00");
            holder.tvOp.setText("￥"+String.valueOf( df.format(op)));
            if (pp!=null)
            {
                holder.tvPp.setText("￥"+String.valueOf(df.format(pp)));
                price=String.valueOf(df.format(pp));
            }
        }
        catch (NumberFormatException nfe) {
            nfe.printStackTrace();
        }



      //  Toast.makeText(mActivity,price,Toast.LENGTH_SHORT).show();
        coursename=list.get(position).getName();
        coursedes=list.get(position).getDes();
        cid=list.get(position).getId();
        String[] strarray= list.get(position).getDes().split(",");
        holder.tvDes1.setText(strarray[0]);
        if(strarray.length>1)
        holder.tvDes2.setText(strarray[1]+"");
        holder.tvname.setText(list.get(position).getName());
        holder.tvDiscount.setText(list.get(position).getDiscount()+"折");
        Log.e("imgurl",list.get(position).getImg());
        Glide.with(mActivity)
                .load(AppConstants.IMG_URL+list.get(position).getCourseType().getImg())
                .error(R.mipmap.kcgm_bj)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(holder.iv_bg);
                holder.btnBay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               // Toast.makeText(mActivity, "-=-----------", Toast.LENGTH_SHORT).show();
                //实例化OrderPopupWindow
                list.get(position).setName2(list.get(position).getCourseType().getName());
                orderPopupWindow = new OrderPopupWindow(mActivity,itemsOnClick,list.get(position));
                rowsBean=list.get(position);
                //显示窗口
               orderPopupWindow.showAtLocation(mActivity.getWindow().findViewById(R.id.main), Gravity.BOTTOM| Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置

            }
        });
        return view;
    }
    static class ViewHolder {
        @BindView(R.id.tv_discount)
        TextView tvDiscount;
        @BindView(R.id.rr)
        RelativeLayout rr;
        @BindView(R.id.tv_des1)
        TextView tvDes1;
        @BindView(R.id.tv_des2)
        TextView tvDes2;
        @BindView(R.id.tv_op)
        TextView tvOp;
        @BindView(R.id.tv_name)
        TextView tvname;
        @BindView(R.id.tv_pp)
        TextView tvPp;
        @BindView(R.id.btn_bay)
        Button btnBay;
        @BindView(R.id.bc)
        RelativeLayout bc;
        @BindView(R.id.iv_bg)
        ImageView iv_bg;
        ViewHolder(View view) {
            ButterKnife.bind(this, view);

        }
    }

    //为弹出窗口实现监听类
    private View.OnClickListener itemsOnClick = new View.OnClickListener(){

        public void onClick(View v) {
            orderPopupWindow.dismiss();
            switch (v.getId()) {
                case R.id.btn_payment:

                    OkGo.post(AppConstants.BASE_URL + "/grade/datalist.shtml")
                            .tag(this)
                            .cacheKey("cacheKey")
                            .cacheMode(CacheMode.DEFAULT)
                            .execute(new StringCallback() {
                                @Override
                                public void onSuccess(String s, Call call, Response response) {
                                    Log.e("历史成绩",s);
                                    Gson gson = new Gson();

                                    HistoricalGrade historicalGrade = gson.fromJson(s, HistoricalGrade.class);
                                    if(historicalGrade.getMsg()!=null)
                                    {
                                        if(historicalGrade.getMsg().equals("timeout"))
                                        {
                                            AppConstants.gotoLogin(mActivity);

                                        }
                                        else if(historicalGrade.getMsg().equals("remote login"))
                                        {
                                            AppConstants.remologin(mActivity);
                                        }

                                    }
                                    else
                                    {
                                        // Toast.makeText(mActivity, "支付", Toast.LENGTH_SHORT).show();
                                        Intent intent = new Intent();
                                        // Toast.makeText(mActivity,price,Toast.LENGTH_SHORT).show();
                                        op=rowsBean.getPrice();
                                        Float count=Float.parseFloat(rowsBean.getDiscount());
                                        pp =count/10*op;
                                        DecimalFormat df   = new DecimalFormat("#0.00");
                                        price=String.valueOf(df.format(pp));
                                        //Toast.makeText(mActivity,price,Toast.LENGTH_SHORT).show();
                                        coursename=rowsBean.getName();
                                        coursedes=rowsBean.getDes();
                                        cid=rowsBean.getId();
                                        intent.putExtra("price",price);
                                        intent.putExtra("coursedes",coursedes);
                                        intent.putExtra("coursename",coursename);
                                        intent.putExtra("cid",cid);
                                        intent.setClass(mActivity, PaymentActivity.class);
                                        mActivity.startActivity(intent);

                                    }


                                }

                                @Override
                                public void onError(Call call, Response response, Exception e) {
                                    super.onError(call, response, e);
                                    Toast.makeText(mActivity, "报告小主，服务器被外星人偷走啦~~", Toast.LENGTH_SHORT).show();
                                }
                            });

                    break;
                default:
                    break;
            }

        }

    };

}
